
function RoundTable(options){
    var defaultOptions = {
        seat: {
            count: 8,
            size: 20,
            offset: 48,
            radius: 5,
            onClick: null
        },
        table: {
            top: 0,
            left: 0,
            size: 56,
            radius: 56 / 2,
            onClick: null
        },
        manager: null
    };

    this.Options = $.extend(true, defaultOptions, options);
}

RoundTable.prototype.appendTo = function(element){
    var instance = this;

    // subclass for callbacks with immutable variables
    function Encapsulate(callback, parameter){
        return function(){
            callback(parameter, instance)
        };
    };

    var table;

    if (this.Table != null){
        table = this.Table;
    } else {
        table = $("<div class='table-round'/>");
        var tableRadius =  this.Options.table.size / 2;
        //table.css("position", "absolute");
        //table.css("top", this.Options.table.x - tableRadius + "px");
        //table.css("left", this.Options.table.y - tableRadius + "px");
        table.css("width", this.Options.table.size + "px");
        table.css("height", this.Options.table.size + "px");
        table.css("background-color", "green");
        table.css("border-radius", this.Options.table.radius);
        table.css("-moz-border-radius", this.Options.table.radius);



        if (this.Options.table.onClick){
            seat.bind("click", this.Options.table.onClick(table, this))
        }

        this.Table = table;

    }

    table.draggable(
        {
            cursor: "move",
            containment: element,
            drag: function(event, ui){
                var x = ui.position.left;
                if (x <= 10){
                    instance.Options.manager.ShowTrash();
                } else {
                    instance.Options.manager.HideTrash();
                }
            },
            stop: function(event, ui){
                var x = ui.position.left;
                if (x <= 10){
                    instance.Table.remove();
                }
            }
        }
    );



    var seats = [];
    if (this.Seats != null){
        seats = this.Seats;
    } else {
        var rotationOffset = 360 / this.Options.seat.count;

        for(var i = 0; i < this.Options.seat.count; i++)
        {
            var seat = $("<div class='seat'/>");
            var rotation = rotationOffset * i;
            var x = Math.round(Math.sin(rotation / 180 * Math.PI) * this.Options.seat.offset) + tableRadius;
            var y = Math.round(Math.cos(rotation / 180 * Math.PI) * this.Options.seat.offset) + tableRadius;
            seat.css("position", "absolute");
            seat.css("top", x - this.Options.seat.size / 2 + "px");
            seat.css("left", y - this.Options.seat.size / 2 + "px");
            seat.css("width", this.Options.seat.size + "px");
            seat.css("height", this.Options.seat.size + "px");
            seat.css("background-color", "red");
            seat.css("border-radius", this.Options.seat.radius + "px");
            seat.css("moz-border-radius", this.Options.seat.radius + "px");
            seat.css("transform", "rotate(" + rotation + "deg)");
            seat.css("-o-transform", "rotate(" + rotation + "deg)");
            seat.css("-ms-transform", "rotate(" + rotation + "deg)");
            seat.css("-moz-transform", "rotate(" + rotation + "deg)");
            seat.css("-webkit-transform", "rotate(" + rotation + "deg)");
            if (this.Options.seat.onClick){
                var onClickEvent = new Encapsulate(instance.Options.seat.onClick, seat);
                seat.bind("click", onClickEvent);
            }
            seats.push(seat);
        }

        this.Seats = seats;
    }

    $.each(seats, function(){table.append(this)});

    $(element).append(table);
};